﻿<template>
  <div class="container activity-child" style="width:99%;margin-top:-25px;">
    <!-- 查询条件 -->
    <div class="query-box">
      <el-form :model="queryBox" status-icon ref="queryBox">
        <el-row>
          <el-col :span="4">
            <el-form-item label prop="name">
              <el-input v-model="queryBox.activityCode" placeholder="请输入内容">
                <template slot="prepend">活动编码</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label prop="name">
              <el-input v-model="queryBox.activityName" placeholder="请输入内容">
                <template slot="prepend">活动名称</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="depart-box">
              <div class="slect-title">分中心名称</div>
              <el-autocomplete
                v-model="centerName"
                :fetch-suggestions="querySearchAsync"
                placeholder="请输入内容"
                @select="handleSelect"
              ></el-autocomplete>
            </div>
          </el-col>
          <el-col :span="4">
            <el-form-item label prop="name">
              <el-input v-model="queryBox.centerCode" placeholder="请输入内容">
                <template slot="prepend">分中心编码</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="from-btns">
        <kt-button
          :label="$t('action.reset')"
          perms="true"
          type="primary"
          @click="reset('queryBox')"
        />
        <kt-button :label="$t('action.search')" perms="true" type="primary" @click="search"/>
      </div>
    </div>
    <div style="float:left;margin-left:2%;margin-bottom:20px;">
      <kt-button :disabled="!loadingData" label="批量导出" perms="activityManage:activity:exportExcel" @click="exportExcel"  type="primary" />
    </div>
    <!--表格内容栏-->
    <div class="btns">
      <Crm-table
        class="table-width"
        activityChild
        width="400"
        :data="pageResult"
        :columns="columns"
        :loading="loading"
        @findPage="findPage"
        @download="download"
        @toSee="toSee"
        :showBatchDelete="false"
        @onError="onError"
        @onCopy="onCopy"
      ></Crm-table>
    </div>

    
    <!-- 查看二维码 -->
    <el-dialog title="查看二维码" width="20%" :visible.sync="addFollow" :close-on-click-modal="false">
      <img :src="url+'/'+imgURL" alt>
      <p>活动名称：{{popData.activeName}}</p>
      <p>活动分中心：{{popData.centerName}}</p>
      <kt-button
        class="stu-btn"
        label="下载二维码"
        perms="true"
        type="primary"
        size="medium"
        @click="download(popData2)"
      />
    </el-dialog>
  </div>
</template>
<script>
import KtButton from "@/views/Core/KtButton";
import CrmTable from "@/views/Core/CrmTable";
import { CodeToText } from "element-china-area-data";
import { baseUrl } from "@/utils/global";
import axios from "axios";
import Cookies from "js-cookie";
export default {
  name: "Xuecoo",
  components: {
    CrmTable,
    KtButton
  },
  data() {
    return {
      isShowFlag: 0, //0:初始化部门、人员不显示  1:部门显示 2：人员显示
      checkedPeson: 0,
      size: "mini",

      // 查询条件
      student_data_from_options: [
        {
          label: " ",
          key: " "
        }
      ],
      centerName: "",
      queryBox: {},
      url: baseUrl,
      addFollow: false, //显示跟进窗口
      width: 200,
      columns: [
        { type: "index", label: "序号", minWidth: 50 },
        { prop: "activeCode", label: "活动编码", minWidth: 90 },
        { prop: "activeName", label: "活动名称", minWidth: 100 },
        { prop: "centerCode", label: "分中心编码", minWidth: 100 },
        { prop: "centerName", label: "分中心名称", minWidth: 100 },
        { prop: "createDate", label: "创建时间", minWidth: 100 },
        { prop: "centerLink", label: "分中心子链接", minWidth: 200 }
      ],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {
        content: []
      },
      loading: false, // 加载标识
      loadingData: false,
      imgURL: "",
      popData: {},
      exportOemUrl:baseUrl+"/activitySublinkManager/batchExport",
    };
  },
  methods: {
    // 搜索
    search() {
      if (JSON.stringify(this.queryBox) == "{}") {
        this.$message({
          message: "请输入筛选项！",
          type: "error"
        });
      } else {
        this.loadingData = true;
        this.loading = true;
        this.pageRequest.pageNum = 1;
        this.findPage(null);
      }
    },
    handleChange(value) {
      
    },
    // 获取分页数据
    findPage: function(data) {
      if (this.loadingData) {
        if (data !== null) {
          this.loading = true;
          this.pageRequest = data.pageRequest;
        }
        this.queryBox.pageSize = this.pageRequest.pageSize;
        this.queryBox.pageNum = this.pageRequest.pageNum;

        this.$api.agent
          .getActivitySublinkManager(this.queryBox)
          .then(res => {
            if (res.result == 2) {
              this.$message({
                message: res.msg,
                type: "error"
              });
            } else {
              let content = {
                content: res.data,
                totalSize: res.total
              };
              this.pageResult = content;
              this.pageResult.content.forEach(item => {
                // 转换状态
                if (item.status == 1) {
                  item.statusInfo = "已生成子链接";
                } else {
                  item.statusInfo = "未生成子链接";
                }
              });
              this.loading = false;
            }
          })
          .then(data != null ? data.callback : "");
      }
    },

    // 重置
    reset(queryBox) {
      this.$refs.queryBox.resetFields();
      this.pageResult = {};
      this.queryBox = {};
      this.centerName = "";
      this.loadingData=false
    },

    // 分中心远程搜索
    querySearchAsync(queryString, callback) {
      let restaurants = [{}];
      let results = "";
      this.$api.dropdown.branchcenter().then(res => {
        for (let i of res.branchCenterInfoList) {
          i.value = i.name; //将想要展示的数据作为value
        }
        restaurants = res.branchCenterInfoList;
        results = queryString
          ? restaurants.filter(this.createFilter(queryString))
          : restaurants;
        callback(results);
      });
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    handleSelect(item) {
      this.queryBox.centerName = item.name;
      // this.queryBox.centerName=item.name
    },
    download: function(params) {
      this.$api.agent.downloadQRCode({ sublinkId: params.row.id }).then(res => {
        if (res.msg != "") {
          this.$message({
            message:res.msg,
            type: "error"
          });
        }else{
          this.downloadIamge(baseUrl + "/" + res.data.path, params.row.centerCode+"_"+params.row.centerName);
        }
      });
    },
    downloadIamge(imgsrc, name) {
      //下载图片地址和图片名
      var image = new Image();
      // 解决跨域 Canvas 污染问题
      image.setAttribute("crossOrigin", "anonymous");
      image.onload = function() {
        var canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据

        var a = document.createElement("a"); // 生成一个a元素
        var event = new MouseEvent("click"); // 创建一个单击事件
        a.download = name || "photo"; // 设置图片名称
        a.href = url; // 将生成的URL设置为a.href属性
        a.dispatchEvent(event); // 触发a的单击事件
      };
      image.src = imgsrc;
    },

    toSee: function(params) {
      this.addFollow = true;
      this.popData = params.row;
      this.popData2=params
      this.$api.agent
        .getQRcode({ sublinkId: params.row.id })
        .then(res => {
          if (res.result == 1) {
            this.imgURL = res.msg;
          }
        })
    },
    onCopy(params) {
      this.$message({
        message: "复制成功" + params.text,
        type: "success"
      });
    },
    onError(params) {
      this.$message({
        message: "复制失败",
        type: "error"
      });
    },
    // 批量导出
    exportExcel(){
      let params = Object.assign({}, this.queryBox);
      axios({
        method:'get',
        url:this.exportOemUrl,
        params:params
      }).then(res=>{
        window.open(res.request.responseURL)
      })
    }
  }
};
</script>
<style >
@import url("../../assets/queryBox.css");
</style>
<style lang='scss' scoped>
.checked-person {
  font-weight: 500;
  display: inline-block;
  text-align: center;
  margin: 3% 0 3% 0;
}
.batchdistribution-pop-tit {
  font-size: 20px;
  font-weight: bold;
  display: inline-block;
  color: #303133;
}
.el-dialog__wrapper .el-form-item__content {
  margin-left: 0;
}
.demo-form-inline {
  width: 90%;
  margin: 0 auto;
  .el-dialog__body .el-input {
    width: 93% !important;
  }
  .el-select {
    width: 100% !important;
  }
  .tit {
    display: inline-block;
    margin-top: 10%;
  }
}
.btn .el-button {
  padding: 9px 5px !important;
}
</style>
<style>
.batchdistribution-pop .el-dialog__header {
  padding: 0 0 0 0 !important;
}
.activity-child .el-dialog__header {
  padding: 20px 0 0 0 !important;
}
.activity-child .el-dialog__body {
  padding: 20px 20px !important;
}
</style>


